//
//  HomeSearch_NavBar.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/11/17.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "HomeSearch_NavBar.h"

@interface HomeSearch_NavBar ()

@property (nonatomic, strong) UIButton *backBtn;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIButton *searchBtn;

@end

@implementation HomeSearch_NavBar

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = HexColor(0xffffff);
        self.backBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, StatusBarHeight, 44, 44)];
        [self.backBtn setImage:[UIImage imageNamed:@"nav_back"] forState:0];
        [self.backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.backBtn];
        
        self.searchBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenW - 70, StatusBarHeight, 70, 44)];
        [self.searchBtn setTitle:@"搜索" forState:0];
        [self.searchBtn setTitleColor:HexColor(0x2B2C44) forState:0];
        self.searchBtn.titleLabel.font = BoldFontOfSize(16);
        [self.searchBtn addTarget:self action:@selector(searchClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.searchBtn];
        
        self.textField = [[UITextField alloc] initWithFrame:CGRectMake(self.backBtn.nim_right, 5 + StatusBarHeight, self.searchBtn.nim_left - self.backBtn.nim_right, 34)];
        self.textField.backgroundColor = HexColor(0xF2F2F5);
        self.textField.layer.masksToBounds = YES;
        self.textField.layer.cornerRadius = 4;
        self.textField.placeholder = @"请输入单号";
        self.textField.font = [UIFont systemFontOfSize:14];
        [self addSubview:self.textField];
        UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, self.textField.nim_height)];
        leftView.backgroundColor = HexColor(0xF2F2F5);
        self.textField.leftView = leftView;
        self.textField.leftViewMode = UITextFieldViewModeAlways;
        
    }
    return self;
}

- (void)backClick {
    if (self.backBlock) {
        self.backBlock();
    }
}

- (void)searchClick {
    if (self.textField.text.length) {
        if (self.searchBlock) {
            self.searchBlock(self.textField.text);
        }
    }
}

-(void)setFieldText:(NSString *)fieldText {
    _fieldText = fieldText;
    self.textField.text = fieldText;
}

@end
